from typing import Dict, Any, Type

from crawlers.base_crawler import BaseCrawler
from crawlers.theknot_wedding_crawler import TheKnotWeddingCrawler
from crawlers.yelp_restaurant_crawler import YelpRestaurantCrawler


class CrawlerFactory:
    """爬虫工厂类，负责创建特定网站的爬虫实例"""
    
    # 注册所有可用的爬虫类型
    _crawler_registry = {
        "theknot_wedding": TheKnotWeddingCrawler,
        "yelp_restaurant": YelpRestaurantCrawler,
    }
    
    @classmethod
    def get_crawler_class(cls, site_name: str) -> Type[BaseCrawler]:
        """获取特定网站的爬虫类
        
        Args:
            site_name: 网站名称
            
        Returns:
            Type[BaseCrawler]: 爬虫类
        """
        crawler_class = cls._crawler_registry.get(site_name)
        if not crawler_class:
            raise ValueError(f"未找到网站 '{site_name}' 的爬虫实现。可用的爬虫: {', '.join(cls._crawler_registry.keys())}")
        return crawler_class
    
    @classmethod
    def create_crawler(cls, site_name: str, config: Dict[str, Any]) -> BaseCrawler:
        """创建特定网站的爬虫实例
        
        Args:
            site_name: 网站名称
            config: 爬虫配置
            
        Returns:
            BaseCrawler: 爬虫实例
        """
        crawler_class = cls.get_crawler_class(site_name)
        return crawler_class(config)
    
    @classmethod
    def register_crawler(cls, site_name: str, crawler_class: Type[BaseCrawler]) -> None:
        """注册新的爬虫类型
        
        Args:
            site_name: 网站名称
            crawler_class: 爬虫类
        """
        cls._crawler_registry[site_name] = crawler_class